﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using EPS.Contracts;
using EPS.Common;
using EPS.UI.Classes;

namespace EPS.UI.ZoneManage
{
    /// <summary>
    /// Interaction logic for ZonePage.xaml
    /// </summary>
    public partial class ZonePage : Page
    {
        ServiceFactory<IProject> ProjectDao = new ServiceFactory<IProject>();
        ServiceFactory<IZone> ZoneDao = new ServiceFactory<IZone>();
        ServiceFactory<ITemplate> TemplateDao = new ServiceFactory<ITemplate>();

        private Project ProjectItem;
        private Zone ZoneItem;

        private int Operate = -1;

        public ZonePage()
        {
            InitializeComponent();
            ProjectItem = new Project();
            ZoneItem = new Zone();
            ProjectExpander.DataContext = ProjectItem;
            ZoneExpander.DataContext = ZoneItem;
            ZoneTree.DataContext = FooViewModel.CreateFoos();
            TemplateComboBox.ItemsSource = TemplateDao.GetProxy().GetBySql("1=1");
        }

        private void btnSaveP_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(ProjectItem.Keyid))
            {
                ProjectItem.Keyid = Guid.NewGuid().ToString();
                ProjectDao.GetProxy().Insert(ProjectItem);
            }
            else
                ProjectDao.GetProxy().Update(ProjectItem);
            ZoneTree.DataContext = FooViewModel.CreateFoos();
            Message.ShowInfo("保存成功!");
        }

        private void btnSaveZ_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(ZoneItem.Keyid))
            {
                ZoneItem.Keyid = Guid.NewGuid().ToString();
                ZoneDao.GetProxy().Insert(ZoneItem);
            }
            else
                ZoneDao.GetProxy().Update(ZoneItem);
            ZoneTree.DataContext = FooViewModel.CreateFoos();
            Message.ShowInfo("保存成功!");
        }

        private void btnDeleteZ_Click(object sender, RoutedEventArgs e)
        {

        }

        private void ZoneTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            var model = e.NewValue as FooViewModel;
            if (model == null) return;
            if (model.DataItem is Project)
            {
                ProjectItem = model.DataItem as Project;
                this.ProjectExpander.DataContext = ProjectItem;
            }
            else if (model.DataItem is Zone)
            {
                ZoneItem = model.DataItem as Zone;
                ZoneExpander.DataContext = ZoneItem;
            }
        }


        private void CanTreeNodeExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = true;

        }

        private void OnTreeNodeExecute(object sender, ExecutedRoutedEventArgs e)
        {
            MenuItem menuItem = e.Parameter as MenuItem;

            switch (menuItem.Name)
            {
                case "NewItemP":
                    ProjectItem = new Project();
                    ProjectExpander.DataContext = ProjectItem;
                    break;
                case "NewPItemZ":
                    var zone = menuItem.Tag as Zone;
                    if (zone != null)
                    {
                        ZoneItem = zone.DeepCopy();
                        ZoneItem.Keyid = ZoneItem.ZoneName = null;
                    }
                    else
                    {
                        var project = menuItem.Tag as Project;
                        ZoneItem = new Zone();
                        ZoneItem.ProjKeyid = project.Keyid;
                        ZoneItem.ZoneNo = "000";
                    }
                    ZoneExpander.DataContext = ZoneItem;
                    break;
                case "NewCItemZ":
                    zone = menuItem.Tag as Zone;
                    ZoneItem = zone.DeepCopy();
                    ZoneItem.Keyid = ZoneItem.ZoneName = null;
                    ZoneItem.ZoneNo += "000";
                    ZoneExpander.DataContext = ZoneItem;
                    break;
                case "DeleteItem":
                    if (Message.ShowOk("确定要删除该节点,以及所有以节点吗?"))
                        if (Delete(menuItem.Tag))
                            ZoneTree.DataContext = FooViewModel.CreateFoos();
                    break;
                default:
                    break;
            }
        }

        private bool Delete(object entity)
        {
            if (entity == null) return false;
            if (entity is Project)
            {
                string keyid = (entity as Project).Keyid;
                ProjectDao.GetProxy().DelById(keyid);
                return true;
            }
            else
            {
                string keyid = (entity as Zone).Keyid;
                ZoneDao.GetProxy().DelById(keyid);
                return true;
            }
        }

        private void ZoneMenu_Opened(object sender, RoutedEventArgs e)
        {
            ContextMenu zoneMenu = sender as ContextMenu;

            if (zoneMenu.Tag is Project)
            {
                (zoneMenu.Items[1] as MenuItem).Header = "添加区域";
                (zoneMenu.Items[2] as MenuItem).Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                (zoneMenu.Items[1] as MenuItem).Header = "添加同级区域";
                var zone = zoneMenu.Tag as Zone;
                if (zone.ZoneNo != null && zone.ZoneNo.Length == 6)
                {
                    (zoneMenu.Items[2] as MenuItem).Visibility = System.Windows.Visibility.Collapsed;
                }
            }
        }
    }
}
